/*****************************************************************************
*                      TestGui for   JMatLink                                *
******************************************************************************
******************************************************************************
* (c) 1999 Stefan Mueller                                                    *
******************************************************************************
*                                                                            *
*  Parameter:         Value:                                                 *
*                                                                            *
******************************************************************************
*                                                                            *
* Public Methods:                                                            *
*                                                                            *
*                                                                            *
******************************************************************************
* 19.01.1999 beginning (reuse HelloWorld example, Java Tutorial       (0.01) *
* 06.02.1999 separation from JMatLink class.                          (0.02) * 
* 02.05.1999 more engGetArray tests                                   (0.03) * 
* 11.09.1999 char array                                               (0.04) *
* 21.09.1999 char array working (double conversion hidden in JMatLink)(0.05) *
* 04/07/2001  restart work on JMatLink                                (0.06) *
* 05/24/2001  engOpenSingleUse                                        (0.06) *
* 06/04/2001 engEvalString(Ep, String)                                       *
******************************************************************************/

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;


public class TestGui extends Frame implements ActionListener{

    MenuBar menubar;                // the menubar
    Menu file, help;                // menu panes
    Choice choice;
    TextArea  eqnTA;             
    Frame   eqnF;
    FileDialog file_dialog;
    
    TextField evalT;

    JMatLink engine;
   
    Button openB;
    Button openSingleUseB;
    TextField engPointerT;
    Button closeB;
    Button closeEngPB;
    Button evalB;
    Button evalEpB;
    Button outputBufferB; 
    Button engPutArrayB;
    Button engGetScalarB, engGetVectorB, engGetArrayB;
    Button testB,charArrayB; 

    Button okay, cancel;            // buttons
    Button exitB;
   
    double testarray[]={1,2,3,4,5.555,6,7,8.1234};
    double testarray2d[][]={{1,2,3},{4,5,6}};
    double[][] array2d; // = new double[4][4];

public TestGui(String title) {
     
    super(title);

    engine = new JMatLink();

    engine.setDebug( false );

    // Create the menubar.  Tell the frame about it.
    menubar = new MenuBar();
    this.setMenuBar(menubar);
    // Create the file menu.  Add two items to it.  Add to menubar.
    file = new Menu("File");
    file.add(new MenuItem("Open"));
    file.add(new MenuItem("Quit"));
    menubar.add(file);
    // Create Help menu; add an item; add to menubar
    help = new Menu("Help");
    help.add(new MenuItem("About"));
    menubar.add(help);
    // Display the help menu in a special reserved place.
    menubar.setHelpMenu(help);
       
    this.setLayout(new FlowLayout());


    // Create pushbuttons
    okay   = new Button("Okay");
    cancel = new Button("Cancel");

    openB            = new Button("Open");
    openSingleUseB   = new Button("OpenSingleUse");
    engPointerT      = new TextField(3);
    closeB           = new Button("Close");
    closeEngPB       = new Button("Close(Ep)");
    exitB            = new Button("Exit"); 

    evalT            = new TextField(40);
    evalB            = new Button("Eval(String)");
    evalEpB          = new Button("Eval(Ep, String)"); 

    outputBufferB    = new Button("engOutputBuffer");

    engPutArrayB     = new Button("engPutArray");

    engGetScalarB    = new Button("engGetScalar");
    engGetVectorB    = new Button("engGetVector");
    engGetArrayB     = new Button("engGetArray");

   testB            = new Button("test function");
   charArrayB       = new Button("charArray(a)");

   openB.addActionListener(this); 
   openSingleUseB.addActionListener(this); 
   closeB.addActionListener(this); 
   closeEngPB.addActionListener(this);
   evalB.addActionListener(this); 
   evalEpB.addActionListener(this); 
   outputBufferB.addActionListener(this);
   engPutArrayB.addActionListener(this); 
   engGetScalarB.addActionListener(this); 
   engGetVectorB.addActionListener(this); 
   engGetArrayB.addActionListener(this); 
   testB.addActionListener(this);
   charArrayB.addActionListener(this);
   exitB.addActionListener(this);
 
   this.add(openB);
   this.add(openSingleUseB);
   this.add(engPointerT);
   this.add(closeB);
   this.add(closeEngPB);
   this.add(evalT);
   this.add(evalB);
   this.add(evalEpB);
   this.add(outputBufferB);
   this.add(engPutArrayB);  
   this.add(engGetScalarB);
   this.add(engGetVectorB);
   this.add(engGetArrayB);
   this.add(testB);
   this.add(charArrayB);

   this.add(okay);
   this.add(cancel);   
   this.add(exitB);

addWindowListener(new WindowAdapter() { public void
         windowClosing(WindowEvent e) { System.exit(0); } } );

}

    

public void actionPerformed(ActionEvent evt) {  
    Object src = evt.getSource();
    String evalS;        
    String outputBufferS;
    double scalar; 
    int    scalarI;
    int    EpI;
    int    retValI;

    if (src==openB) 
    { 
        System.out.println("engOpen()"); 
        EpI = engine.engOpen();
        engPointerT.setText(new Integer(EpI).toString());
    }
    if (src==openSingleUseB)
    { 
        System.out.println("engOpenSingleUse()");
        EpI = engine.engOpenSingleUse("startCmd=55;");
        engPointerT.setText(new Integer(EpI).toString());
    }
    if (src==closeB) 
    {
        engine.engClose();
    }
    if (src==closeEngPB)
    {
       EpI = Integer.parseInt(engPointerT.getText());
       System.out.println("engClose (" + EpI +")" );
       engine.engClose( EpI );
    }

    if (src==evalB) 
    {
        evalS = evalT.getText();
        System.out.println("engEvalString "+evalS);
        engine.engEvalString(evalS);
    }

    if ( src == evalEpB ) 
    {
        evalS = evalT.getText();
        EpI = Integer.parseInt(engPointerT.getText());
        System.out.println("engEvalString "+ EpI +" "+ evalS);
        engine.engEvalString(EpI, evalS);
    }

    if (src==outputBufferB) {
       System.out.println("outputBufferB");
       outputBufferS = engine.engOutputBuffer();
       System.out.println(outputBufferS);
    }
    if (src==engPutArrayB) {
       System.out.println("engPutArray");
       System.out.println("1x1");
       engine.engPutArray("a",1);
       System.out.println("1xn");
       engine.engPutArray("b",testarray);
       System.out.println("nxn");
       engine.engPutArray("c",testarray2d);
    }
    if (src==engGetScalarB) {
       System.out.println("engGetScalar");
       scalar = engine.engGetScalar( "a" );
       System.out.println("scalar "+scalar);
       evalT.setText(new Double(scalar).toString());
    }
    if (src==engGetArrayB) {
       System.out.println("engGetArray");
       array2d = engine.engGetArray( "a" );
       evalT.setText(new Double(array2d[0][0]).toString());
       System.out.println("length "+ array2d[1].length);
       System.out.println("length "+ array2d.length);
       System.out.println(array2d[0][0]+" "+array2d[0][1]+" "+array2d[0][2]);
       System.out.println(array2d[1][0]+" "+array2d[1][1]+" "+array2d[1][2]);
       System.out.println(array2d[2][0]+" "+array2d[2][1]+" "+array2d[2][2]);
    }
    if (src==testB) {
       array2d = engine.engGetArray( "a" );
       System.out.println("size "+array2d.length+" "+array2d[0].length);
       engine.engPutArray("b",array2d);
    }
    if (src==charArrayB) {
        String[] aS = engine.engGetCharArray("a");
        for (int i=0; i<aS.length; i++) 
            System.out.println("engGetCharArray "+i+" "+aS[i]);
    }
    if (src==exitB) {
        engine.engClose();
        engine.kill();
        System.exit(0);
        //this.dispose();
    }
    repaint();
} // actionPerformed

    

public static void main(String[] args) {
        Frame f = new TestGui("JavaMatLink");
        f.setSize(500, 250);
        f.show();
}

}
